package com.apes.fn.supp.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.fn.supp.model.SantouChannel;
import com.apes.fn.supp.repository.SantouChannelRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class SantouChannelService extends DomainService {

    @Autowired
    private SantouChannelRepository santouChannelRepository;

    /**
     * 查询所有数据
     * @param request
     * @return
     */
    public Page<SantouChannel> conditionQuery(SimpleRequest request){
        List<Map> filters = request.get("condition");
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return santouChannelRepository.findAll(JpaDsl.toCriteria(filters), PageRequest.of(request.get("page"), request.get("size"), sort));
    }

    /**
     * 三头六臂渠道新增 修改
     * @param request
     * @return
     */
    public SantouChannel save(SimpleRequest request){
        JSONObject jo = request.getJO();
        SantouChannel santouChannel = new SantouChannel();
        santouChannel.setId(jo.getString("id"));
        santouChannel.setVersion(jo.getInteger("version")==null?0:jo.getInteger("version"));
        santouChannel.setChannelNumber(jo.getString("channelNumber"));
        santouChannel.setChannelName(jo.getString("channelName"));
        santouChannel.setSupplier(jo.getObject("supplier",PartyRole.class));
        return santouChannelRepository.saveAndFlush(santouChannel);
    }


    /**
     * 三头六臂渠道删除
     * @param request
     * @return
     */
    public void  delete(SimpleRequest request){
        SantouChannel santouChannel = request.getO(SantouChannel.class);
        santouChannelRepository.delete(santouChannel);
    }

}
